package com.erbao.dp.commandandcor;

import com.erbao.dp.command.*;

import java.util.ArrayList;
import java.util.List;

class CommandChain extends Command{
    private int index;
    private List<Command> commandList;

    public CommandChain() {
        commandList = new ArrayList<>();
    }

    public boolean registryCommand(Command command) {
        return commandList.add(command);
    };

    @Override
    public void doit() {
        while (index < commandList.size()) {
            commandList.get(index++).doit();
        };
    }

    @Override
    public void undo() {
        while (index > 0){
            commandList.get(--index).undo();
        }
    }
}



/**
 * @author CaoBaoYin
 * @since 21/9/28 17:54
 */
public class Main {

    public static void main(String[] args) {
        CommandChain chain = new CommandChain();
        Content c = new Content();
        chain.registryCommand(new CopyCommand(c));
        chain.registryCommand(new InsertCommand(c));
        chain.registryCommand(new DeleteCommand(c));

        chain.doit();
        chain.undo();

        System.out.println(c.msg);
    }


}
